package com.sl.plugin.step.selenium.keys;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.sl.core.engine.meta.impl.AbstractProcessCompDescriptor;
import com.sl.core.engine.step.ProcessCompService;
import com.sl.core.engine.step.impl.AbstractProcessComp;
import org.apache.commons.compress.utils.Lists;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import java.util.List;

/**
 */
public class SeleniumKeysComp extends AbstractProcessComp<WebDriver, WebDriver> implements ProcessCompService {


    public SeleniumKeysComp(AbstractProcessCompDescriptor abstractProcessCompDescriptor) {
        super(abstractProcessCompDescriptor);
    }


    @Override
    public WebDriver input(WebDriver driver) {

        String cssSelector = meta.getStrAttr("cssSelector");

        List<String> key = meta.getListAttr("");
        if (StrUtil.isBlank(cssSelector)) {
            cssSelector = "html";
        }
        if (StrUtil.isNotBlank(cssSelector)) {
            List<String> st = Lists.newArrayList();
            for (String s : key) {
                Keys keys = null;
                try {
                    keys =Keys.valueOf(s.toUpperCase());
                }catch (Exception e){

                }
                if (ObjectUtil.isNull(keys)) {
                    st.add(s);
                } else {
                    st.add(keys.toString());
                }
            }

            driver.findElement(By.cssSelector(cssSelector)).sendKeys(st.toArray(new String[0]));
        }
        return driver;
    }

}
